package com.zyw.service.clothing.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyw.entity.ClothingCategory;
import com.zyw.enums.ManagermentSystemEnum;
import com.zyw.exception.ManagermentSystemException;
import com.zyw.from.ClothingFrom;
import com.zyw.mapper.clothing.ClothingCategoryMapper;
import com.zyw.service.clothing.ClothingCategoryService;
import com.zyw.vo.LayUITableVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class ClothingCategoryServiceImpl implements ClothingCategoryService {

    @Resource
    public ClothingCategoryMapper clothingCategoryMapper;

    @Override
    public List<ClothingCategory> selectAll() {
        return clothingCategoryMapper.selectAll();
    }

    @Override
    public List<ClothingCategory> findBySex(Integer sexType) {

        Example example = new Example(ClothingCategory.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("sex",sexType);
        List<ClothingCategory> clothingCategoryList = clothingCategoryMapper.selectByExample(example);

        return clothingCategoryList;
    }

    @Override
    public LayUITableVO<List<ClothingCategory>> findPage(Map<String, Object> map) {
        ClothingFrom from = (ClothingFrom  ) map.get("from");

        PageHelper.startPage((int) map.get("page"),(int) map.get("limit"));

        Example example = new Example(ClothingCategory.class);
        Example.Criteria c = example.createCriteria();
        if (from.getSexType()!=null){
            c.andEqualTo("sex",from.getSexType());
        }


        if (! "".equals(from.getCategoryName()) && null!=from.getCategoryName()){
            c.andLike("name","%"+from.getCategoryName()+"%");
        }

        List<ClothingCategory> list = clothingCategoryMapper.selectByExample(example);
        for (ClothingCategory clothingCategory : list) {
            if (clothingCategory.getSex() == 0){
                clothingCategory.setSexString("女装");
            }else {
                clothingCategory.setSexString("男装");
            }
        }

        PageInfo<ClothingCategory> pageInfo = new PageInfo<>(list);

        LayUITableVO<List<ClothingCategory>> vo = new LayUITableVO<>();
        vo.setCount(pageInfo.getTotal());
        vo.setData(pageInfo.getList());

        return vo;
    }

    @Override
    public ClothingCategory selectById(String id) {
        return clothingCategoryMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void update(ClothingCategory clothingCategory) {
        Example example = new Example(ClothingCategory.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("name",clothingCategory.getName());
        c.andEqualTo("sex",clothingCategory.getSex());

        List<ClothingCategory> clothingCategories = clothingCategoryMapper.selectByExample(example);
        if (clothingCategories.size()>0){
            throw new ManagermentSystemException(ManagermentSystemEnum.FOOD_CATEGORY_ADD_REPETITION);
        }

        clothingCategoryMapper.updateByPrimaryKeySelective(clothingCategory);

    }

    @Override
    @Transactional
    public void save(ClothingCategory clothingCategory) {
        Example example = new Example(ClothingCategory.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("name",clothingCategory.getName());
        c.andEqualTo("sex",clothingCategory.getSex());

        List<ClothingCategory> clothingCategories = clothingCategoryMapper.selectByExample(example);
        if (clothingCategories.size()>0){
            throw new ManagermentSystemException(ManagermentSystemEnum.FOOD_CATEGORY_ADD_REPETITION);
        }
        clothingCategoryMapper.insertSelective(clothingCategory);
    }

    @Override
    @Transactional
    public void delelteById(String id) {
        clothingCategoryMapper.deleteByPrimaryKey(id);
    }
}
